package com.example.administrator.runbook;

import android.app.Application;
import android.os.Environment;
import android.util.Log;

import com.example.administrator.runbook.findfragment.Utils;
import com.example.administrator.runbook.findfragment.model.FileCache;
import com.example.administrator.runbook.fragments.community.utils.OkHttp3Downloader;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.EventBusBuilder;

import java.io.File;

/**
 * Created by 俊峰 on 2016/11/8.
 */

public class MainApplication extends Application {

    private File allFile;

    private static MainApplication application;
    @Override
    public void onCreate() {
        super.onCreate();

        //EventBus注册
        EventBusBuilder builder = EventBus.builder();
        builder.installDefaultEventBus();

        //Picasso
        File file = new File(getExternalCacheDir(), "img");
        if (!file.exists()) {
            file.mkdirs();
        }
        new Picasso.Builder(this)
                .downloader(new OkHttp3Downloader(file))
                .build();
        application = this;

        createFile();
    }

    public static  MainApplication getInstance() {
        return application;
    }

    private void createFile() {

        allFile = new File(Environment.getExternalStorageDirectory(), FileCache.MyAllPath);
        if (!allFile.exists()) {
            if (allFile.mkdir()) {
                FileCache.mFileCache = new File(allFile, FileCache.FileCachePath);
                FileCache.mImageCache = new File(allFile, FileCache.ImageCachePath);
                FileCache.mVideoCache = new File(allFile, FileCache.VideoCachePath);
                createContentPath();
            }
        } else {
            FileCache.mFileCache = new File(allFile, FileCache.FileCachePath);
            FileCache.mImageCache = new File(allFile, FileCache.ImageCachePath);
            FileCache.mVideoCache = new File(allFile, FileCache.VideoCachePath);
            createContentPath();
        }

    }

    private void createContentPath() {
        if (!FileCache.mFileCache.exists()) {
            if (FileCache.mFileCache.mkdir()) {
                Log.d("Hello", "createMyApplicationFile: CreateFileCache is Success");
            }
        }
        if (!FileCache.mVideoCache.exists()) {
            if (FileCache.mVideoCache.mkdir()) {
                Log.d("Hello", "createMyApplicationFile: CreateVideoCache is Success");
            }
        }
        if (!FileCache.mImageCache.exists()) {
            if (FileCache.mImageCache.mkdir()) {
                Log.d("Hello", "createMyApplicationFile: CreateImageCache is Success");
            }
        }
    }
}
